package com.karst.essentials.chapter06
{
	import caurina.transitions.Tweener;
	
	import flash.events.Event;
	
	import org.papervision3d.events.FileLoadEvent;
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.materials.BitmapFileMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.view.BasicView;
	
	internal class TweenerCubeExample extends InteractionTemplate
	{
		public function TweenerCubeExample()
		{
			super( true );
			buttonMode = true ;
			useHandCursor = true ;
		}
		
		private var cube : Cube = null ;
		override protected function createView():void
		{
			if( cube == null )
			{
				var frontMaterial : BitmapFileMaterial = new BitmapFileMaterial( "01.jpg" ) ;
				var backMaterial : BitmapFileMaterial = new BitmapFileMaterial( "01.jpg" ) ;
				var leftMaterial : BitmapFileMaterial = new BitmapFileMaterial( "map.jpg" ) ;
				var rightMaterial : BitmapFileMaterial = new BitmapFileMaterial( "map.jpg" ) ;
				var topMaterial : BitmapFileMaterial = new BitmapFileMaterial( "earth.jpg" ) ;
				var bottomMaterial : BitmapFileMaterial = new BitmapFileMaterial( "earth.jpg" ) ;
				frontMaterial.interactive = true ;
				backMaterial.interactive = true ;
				leftMaterial.interactive = true ;
				rightMaterial.interactive = true ;
				topMaterial.interactive = true ;
				bottomMaterial.interactive = true ;
				
				var materils : MaterialsList = new MaterialsList() ;
				materils.addMaterial( frontMaterial, "front" ) ;
				materils.addMaterial( backMaterial, "back" ) ;
				materils.addMaterial( leftMaterial, "left" ) ;
				materils.addMaterial( rightMaterial, "right" ) ;
				materils.addMaterial( topMaterial, "top" ) ;
				materils.addMaterial( bottomMaterial, "bottom" ) ;
				
				cube = new Cube( materils, 240, 240, 240, 4, 4, 4 ) ;
				cube.localRotationY = 90 ;
			}
			scene.addChild( cube ) ;
			camera.z = -700 ;
			cube.addEventListener( InteractiveScene3DEvent.OBJECT_CLICK, clickCubeHandler ) ;
		}
		
		private function clickCubeHandler( event : InteractiveScene3DEvent ) : void
		{
			if( !Tweener.isTweening( event.displayObject3D ) ) 
			{
				Tweener.addTween( event.displayObject3D, 
					{ localRotationY : event.displayObject3D.localRotationY+90, time : 1 } ) ;
			}
		}
		
		override protected function onRenderTick(event:Event=null):void
		{
			super.onRenderTick( event ) ;
			if( cube )
				cube.localRotationZ += 1 ;
		}
	}
}